import "./style.css";
import "./assets/font.css"
import { createApp } from "vue";
import App from "./App.vue";
import router from './router'
import {
  // create naive ui
  create,
  // component
  NButton,
  NRate,
  NUploadDragger,
  NUpload,
  NSlider,
  NMessageProvider,
  NPagination,
  NIcon,
  NMenu,
  NForm,
  NFormItem,
  NInput,
  NSelect,
  NDataTable,
  NEllipsis,
  NMarquee,
  createDiscreteApi,
  NTooltip,
  NDatePicker
} from "naive-ui";
import { createPinia } from "pinia";


import { useUserStore } from './store/user';

const naive = create({
  components: [
    NButton,
    NUpload,
    NDatePicker,
    NUploadDragger,
    NSlider,
    NRate,
    NMessageProvider,
    NPagination,
    NIcon,
    NMenu,
    NForm,
    NFormItem,
    NInput,
    NSelect,
    NDataTable,
    NEllipsis,
    NMarquee,
    NTooltip
  ],
});
const pinia = createPinia();


const { dialog } = createDiscreteApi(['message','dialog'])

const whitelistPage= ['login','register']
router.beforeEach(async (to, _, next) => {
  const userStore = useUserStore();
  const token = localStorage.getItem('token');
  
  if (!token && !whitelistPage.includes(to.name as string)) {
    
    // 判断是否是本地模式
    if(localStorage.getItem('storageEngine')!='local') {
      dialog.info({
        'title':'提示',
        'content':'当前数据存储为云端模式，请先登录',
        'onPositiveClick':() => {
          
        },
        'positiveText':'确认'
      })
      next({ name: 'login' });

    } else {
      next()
    }

  } else {
    if (token) {
      await userStore.fetchUserInfo();
    }
    next();
  }
});

const app = createApp(App).use(pinia).use(naive).use(router);

app.mount("#app");
